#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>

using namespace std;


int get_mtime(timeval start, timeval end)
{
	long seconds  = end.tv_sec  - start.tv_sec;
	long useconds = end.tv_usec - start.tv_usec;
	return ((seconds) * 1000 + useconds/1000.0) + 0.5;
}

int main(int argc, const char **argv)
{
	struct timeval start, end;
	int repeats = 1;
	bool print_resutls = false;
	
	//process command line arguments
	for (int i = 1; i < argc; i++) {
		std::string option(argv[i]);
		if (option == "-h") {
			std::cout << "usage: a.out [-n] <num of repeats> [-p]\n";
			exit(1);
		}
		else if (option == "-n") {
			i++;
			if (i >= argc || atoi(argv[i]) < 1 ) {
				std::cout << "bad input params" << std::endl;
				exit(1);
			}
			repeats = atoi(argv[i]);
		}
		else if (option == "-p") {
			print_resutls = true;
		}
		else {
			std::cout << "Invalid input parameters." << std::endl;
			exit(1);
		}
	}
	    

	system("echo \"\" > tmp.txt");
	//run queries for table manufacturers
	std::cout << "Running test queries for table manufacturers" << std::endl;
	long sprql_man = 0, sql_man = 0;
	gettimeofday(&start, NULL);
	
	for (int i = 0; i < repeats; i++) {
		if (print_resutls)
			system("arq --query manufacturer.spq --data dummy.ttl");
		else {
			system("arq --query manufacturer.spq --data dummy.ttl >> tmp.txt");
		}
	}
	gettimeofday(&end, NULL);
	sprql_man = get_mtime(start, end);
	cout << "Manufacturer SPRQL Elapsed time: " << sprql_man << " milliseconds\n";
	
	//time sql query
	gettimeofday(&start, NULL);
	for (int i = 0; i < repeats; i++) {
		if (print_resutls)
			system("./manufacturer.sql");
		else
			system("./manufacturer.sql >> tmp.txt");
	}
	
	gettimeofday(&end, NULL);
	sql_man = get_mtime(start, end);
	cout << "Manufacturer  SQL Elapsed time: " << sql_man << " milliseconds\n";
	
	
	
	//run queries for table companies
	std::cout << "Running test queries for table companies" << std::endl;
	long sprql_comp = 0, sql_comp = 0;
	gettimeofday(&start, NULL);
	for (int i = 0; i < repeats; i++) {
		if (print_resutls)
			system("arq --query company.spq --data dummy.ttl");
		else
			system("arq --query company.spq --data dummy.ttl >> tmp.txt");
	}	
	gettimeofday(&end, NULL);
	sprql_comp = get_mtime(start, end);
	cout << "Companies SRQL total elapsed time: " << sprql_comp << " milliseconds\n";
	
	//time sql query
	gettimeofday(&start, NULL);
	for (int i = 0; i < repeats; i++) {
		if (print_resutls)
			system("./company.sql");
		else
			system("./company.sql >> tmp.txt");
	}	
	gettimeofday(&end, NULL);
	sql_comp = get_mtime(start, end);
	cout << "Companies SQL total elapsed time: " << sql_comp << " milliseconds\n";
	
	
	//run queries for table people
	std::cout << "Running test queries for table people" << std::endl;
	long sprql_ppl = 0, sql_ppl = 0;
	//time sprql query
	gettimeofday(&start, NULL);
	for (int i = 0; i < repeats; i++) {
		if (print_resutls)
			system("arq --query people.spq --data dummy.ttl");
		else
			system("arq --query people.spq --data dummy.ttl >> tmp.txt");
	}
	gettimeofday(&end, NULL);
	sprql_ppl = get_mtime(start, end);
	cout << "People SPRQL total elapsed time: " << sprql_ppl << " milliseconds\n";
	
	//time sql query
	gettimeofday(&start, NULL);
	for (int i = 0; i < repeats; i++) {
		if (print_resutls)
			system("./people.sql");
		else {
			system("./people.sql >> tmp.txt");
		}
	}
	gettimeofday(&end, NULL);
	sql_ppl = get_mtime(start, end);
	cout << "People SQL total elapsed time: " << sql_ppl << " milliseconds\n";
	
	std::cout << "************RESULTS***********\n" << std::endl;
	std::cout << "manufacturers table queries - average response time: " << std::endl;
	std::cout << "SPARQL - " << sprql_man/repeats << std::endl;
	std::cout << "SQL - " << sql_man/repeats << std::endl;
	
	std::cout << "\ncompanies table queries - average response time: " << std::endl;
	std::cout << "SPARQL - " << sprql_comp/repeats << std::endl;
	std::cout << "SQL - " << sql_comp/repeats << std::endl;
	
	std::cout << "\npeople table queries - average response time: " << std::endl;
	std::cout << "SPARQL - " << sprql_ppl/repeats << std::endl;
	std::cout << "SQL - " << sql_ppl/repeats << std::endl;
	std::cout << std::endl;
	
	
	
	return 0;
}